import 'package:f200/common/index.dart';
import 'package:f200/features/cart/application/controller/shopping_screen_controller.dart';
import 'package:f200/features/categroy/application/controller/category_screen_controller.dart';
import 'package:f200/features/member/application/controller/mine_page_controller.dart';
import 'package:f200/features/theme/application/controller/theme_controller.dart';
import 'package:f200/pages/home/controllers/home_view_controller.dart';
import 'package:f200/routes/routes.dart';
import 'package:f200/system/index/controllers/index_controller.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await applicationInit();

  runApp(const MyApp());
}

Future<void> applicationInit() async {
  screenSize.setup(width: 360, height: 640);

  await Get.putAsync(() async => await StorageService().init());

  Get.lazyPut(() => MinePageController());
  Get.lazyPut(() => IndexController());
  Get.lazyPut(() => HomeViewController());
  Get.lazyPut(() => CategoryScreenController());
  Get.lazyPut(() => ShoppingScreenController());
  Get.lazyPut(() => UserProfileStorage());
  Get.lazyPut(() => ThemeController());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    screenSize.init(context);
    Loading.init(context);

    return GetMaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      defaultTransition: Transition.rightToLeft,
      transitionDuration: Duration(milliseconds: 150),
      enableLog: true,
      logWriterCallback: logWriterCallback,
      getPages: Routes.pages,
      initialRoute: RouteNames.Main.name,
      builder: (context, child) {
        debugPrint(
            '${NumberFormatter(number: 13242432.110, pattern: '\u00A4 ##,###.0#')}');
        debugPrint('${DateTimeFormatter(dateTime: DateTime.now())}');
        debugPrint(
            '${DateTimeFormatter(dateTime: DateTime.now(), pattern: "yyyy:MM:dd E hh:mm:ss a")}');
        logD(
            '监听到安卓系统字体大小变化:${MediaQuery.of(context).textScaleFactor.toString()}');
        logD(
            '监听到安卓系统主题模式切换:${MediaQuery.of(context).platformBrightness == Brightness.light ? '白色主题' : '深色主题'}');
        logD('设置App文字缩放因子：1，不随系统变化');
        return MediaQuery(
          // 设置全局文字缩放因子为默认1.0,文字大小不随系统变化而改变
          data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
          child: child!,
        );
      },
    );
  }
}
